package Fragment;

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hjh.qinxin.R;

import java.util.ArrayList;
import java.util.List;

import adpter.FmmAdapter;
import adpter.ReportListAdapter;
import model.ReportListModel;
import retrofit2.Call;
import retrofit2.Callback;
import web.BaseApplication;
import web.MediaReport.MeReResponse;
import web.MediaReport.ValuesMeRe;
import web.StarBrand.StarResponse;
import web.StarBrand.ValuesStar;


public class BrandFragment extends Fragment {
    private LinearLayout ll_list_default;
    private TextView ll_list_default_txt;
    private ImageView ll_list_default_icon;

    private LinearLayout ll_list_brand;
    private TextView list_brand_txt;
    private ImageView ll_list_brand_icon;

    private LinearLayout list_list_type;
    private TextView list_list_type_txt;
    private ImageView list_list_type_icon;

    private RecyclerView recyclerView;
    private FmmAdapter<String> popAdapter, popAdapter1, popAdapter2;
    private List<String> popList, popList1, popList2;
    private PopupWindow popupWindow;
    private String currentDefault, currentBrand,currentType;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_brand, container, false);
        StarBrandList(root);
        InitView();

        ll_list_default = root.findViewById(R.id.ll_list_default);
        ll_list_default_txt = root.findViewById(R.id.ll_list_default_txt);
        ll_list_default_icon = root.findViewById(R.id.ll_list_default_icon);

        ll_list_brand = root.findViewById(R.id.ll_list_brand);
        list_brand_txt = root.findViewById(R.id.list_brand_txt);
        ll_list_brand_icon = root.findViewById(R.id.ll_list_brand_icon);

        list_list_type = root.findViewById(R.id.list_list_type);
        list_list_type_txt = root.findViewById(R.id.list_list_type_txt);
        list_list_type_icon = root.findViewById(R.id.list_list_type_icon);


        ll_list_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ll_list_default_txt.setTextColor(Color.parseColor("#00d8a0"));
                ll_list_default_icon.setImageResource(R.mipmap.screen_icon_selected);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setAdapter(popAdapter);
                popupWindow.showAsDropDown(ll_list_default);
            }
        });
        ll_list_brand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //list_brand_txt.setTextColor(Color.parseColor("#00d8a0"));
                ll_list_brand_icon.setImageResource(R.mipmap.screen_icon_selected);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setAdapter(popAdapter1);
                popupWindow.showAsDropDown(ll_list_default);
            }
        });

        list_list_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //list_list_type_txt.setTextColor(Color.parseColor("#00d8a0"));
                list_list_type_icon.setImageResource(R.mipmap.screen_icon_selected);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setAdapter(popAdapter2);
                popupWindow.showAsDropDown(ll_list_default);
            }
        });
        return root;
    }
    private void InitView() {
        popList = new ArrayList<>();
        popList.add("全部地区");
        popList.add("安徽");
        popList.add("北京");
        popList.add("重庆");
        popList.add("福建");
        popList.add("广东");
        popList.add("甘肃");
        popList.add("广西");
        popList.add("贵州");
        popList.add("海南");
        popList.add("河北");
        popList.add("河南");
        popList.add("黑龙江");
        popList.add("湖北");
        popList.add("湖南");
        popList.add("吉林");
        popList.add("江苏");
        popList.add("江西");
        popList.add("辽宁");
        popList.add("内蒙古");
        popList.add("宁夏");
        popList.add("青海");
        popList.add("陕西");
        popList.add("四川");
        popList.add("山东");
        popList.add("上海");
        popList.add("山西");
        popList.add("天津");
        popList.add("新疆");
        popList.add("西藏");
        popList.add("云南");
        popList.add("浙江");
        popList.add("其他");

        popList1 = new ArrayList<>();
        popList1.add("全部行业");
        popList1.add("采矿业");
        popList1.add("制造业");
        popList1.add("电力、热力、燃气、及水生产和供应业");
        popList1.add("建筑业");
        popList1.add("批发和零售业");
        popList1.add("交通运输、仓储和邮政业");
        popList1.add("住宿和餐饮业");
        popList1.add("信息传输、软件和信息技术服务业");
        popList1.add("金融业");
        popList1.add("房地产业");
        popList1.add("租赁和商务服务业");
        popList1.add("科学研究和技术服务业");
        popList1.add("水利，环境和公共设施管理业");
        popList1.add("居民服务、修理和其他服务业");
        popList1.add("教育");
        popList1.add("卫生和社会工作");
        popList1.add("文化、体育和娱乐业");
        popList1.add("公共管理、社会保障和社会组织");
        popList1.add("国际组织");
        popList1.add("其他");

        popList2 = new ArrayList<>();
        popList2.add("默认排序");
        popList2.add("按时间正序");
        popList2.add("按时间倒序");

        View contentView = getLayoutInflater().inflate(R.layout.popwin_supplier_list, null);
        popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, false);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });

        recyclerView = contentView.findViewById(R.id.popwin_supplier_list_lv);
        contentView.findViewById(R.id.popwin_supplier_list_bottom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popAdapter = new FmmAdapter<>(R.layout.item_listview_popwin, popList);
        popAdapter.setOnCallBackData(new FmmAdapter.OnCallBackData<String>() {
            @Override
            public void convertView(BaseViewHolder holder, String item) {
                ((TextView) holder.getView(R.id.listview_popwind_tv)).setText(item);
                if (ll_list_default_txt.getText().toString().trim().equals(item)) {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#00d8a0"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.VISIBLE);
                } else {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#333333"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.GONE);
                }
            }
        });

        popAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //后期自己做请求处理
                popupWindow.dismiss();
                currentDefault = popList.get(position);
                ll_list_default_txt.setText(currentDefault);
            }
        });

        popAdapter1 = new FmmAdapter<>(R.layout.item_listview_popwin, popList1);
        popAdapter1.setOnCallBackData(new FmmAdapter.OnCallBackData<String>() {
            @Override
            public void convertView(BaseViewHolder holder, String item) {
                ((TextView) holder.getView(R.id.listview_popwind_tv)).setText(item);
                if (ll_list_default_txt.getText().toString().trim().equals(item)) {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#00d8a0"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.VISIBLE);
                } else {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#333333"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.GONE);
                }
            }
        });

        popAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //后期自己做请求处理
                popupWindow.dismiss();
                currentBrand = popList1.get(position);
                list_brand_txt.setText(currentBrand);
            }
        });


        popAdapter2 = new FmmAdapter<>(R.layout.item_listview_popwin, popList2);
        popAdapter2.setOnCallBackData(new FmmAdapter.OnCallBackData<String>() {
            @Override
            public void convertView(BaseViewHolder holder, String item) {
                ((TextView) holder.getView(R.id.listview_popwind_tv)).setText(item);
                if (list_list_type_txt.getText().toString().trim().equals(item)) {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#00d8a0"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.VISIBLE);
                } else {
                    ((TextView) holder.getView(R.id.listview_popwind_tv)).setTextColor(Color.parseColor("#333333"));
                    holder.getView(R.id.iv_select_icon).setVisibility(View.GONE);
                }
            }
        });

        popAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //后期自己做请求处理
                popupWindow.dismiss();
                currentType = popList2.get(position);
                list_list_type_txt.setText(currentType);
            }
        });



    }
    private void StarBrandList(View root){
        List<ReportListModel> data  =new ArrayList<>();
        RecyclerView recyclerView = root.findViewById(R.id.brand_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        ReportListAdapter adapter = new ReportListAdapter(getActivity());
        adapter.setData(data);
        recyclerView.setAdapter(adapter);
        Call<StarResponse<ValuesStar>> call = BaseApplication.httpbinService.StarBrand();
        call.enqueue(new Callback<StarResponse<ValuesStar>>() {
            @Override
            public void onResponse(Call<StarResponse<ValuesStar>> call, retrofit2.Response<StarResponse<ValuesStar>> response) {
                for (int i = 0;i < response.body().getValues().getModel().size();i++){
                    ReportListModel item = new ReportListModel();
                    item.setUrl(response.body().getValues().getModel().get(i).getUrl());
                    item.setListHeadline(response.body().getValues().getModel().get(i).getPinpai());
                    item.setListTime(response.body().getValues().getModel().get(i).getCompany_name());
                    data.add(item);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<StarResponse<ValuesStar>> call, Throwable t) {

            }
        });

    }

}